package cn.botter.subject.domain.handler.subject.impl;

import cn.botter.subject.common.enums.IsDeletedEnum;
import cn.botter.subject.common.enums.SubjectInfoTypeEnum;
import cn.botter.subject.domain.convert.RadioSubjectConverter;
import cn.botter.subject.domain.entity.SubjectAnswerBO;
import cn.botter.subject.domain.entity.SubjectInfoBO;
import cn.botter.subject.domain.handler.subject.SubjectTypeHandler;
import cn.botter.subject.infra.basic.entity.SubjectBrief;
import cn.botter.subject.infra.basic.entity.SubjectRadio;
import cn.botter.subject.infra.basic.service.SubjectRadioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
@Slf4j
@Component
public class RadioTypeHandler implements SubjectTypeHandler {
    @Resource
    private SubjectRadioService subjectRadioService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //单选题目的插入
        Integer SubjectId = subjectInfoBO.getId();
        List<SubjectRadio> subjectRadios = new LinkedList<>();
        List<SubjectAnswerBO> optionList = subjectInfoBO.getOptionList();
        if (CollectionUtils.isEmpty(optionList)){
            log.info("Subject.Radio.add options isEmpty!");
            return;
        }
        optionList.forEach(option->{
            SubjectRadio subjectRadio = RadioSubjectConverter.INSTANCE.convertBoToRadio(option);
            subjectRadio.setSubjectId(SubjectId);
            subjectRadio.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
            subjectRadios.add(subjectRadio);
        });

        subjectRadioService.bathInsert(subjectRadios);
    }

    @Override
    public SubjectInfoBO query(Integer id) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(id);
        List<SubjectRadio> result = subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOList = RadioSubjectConverter.INSTANCE.convertRadioToAnswerBO(result);
        SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
        subjectInfoBO.setOptionList(subjectAnswerBOList);

        return subjectInfoBO;
    }
}
